package com.csii.pos.caacctewsu;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.csii.adhoc.annotation.OperationLog;
import com.csii.adhoc.controller.BaseController;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.csii.adhoc.util.ConvertUtils;
import com.csii.pos.caacctewsu.service.ICaAcctEwSuService;
import com.csii.pos.caacctewsu.vo.CaAcctEwSuVO;
import com.csii.result.APIResult;
import com.csii.result.PageVo;
import lombok.extern.slf4j.Slf4j;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;


/**
 * @Description: 账户预警管理
 * @Author: power-stdio
 * @Date:   2021-07-08
 * @Version: V1.0
 */

@RestController
@RequestMapping("/caAcctEwSu")
@Slf4j
public class CaAcctEwSuController extends BaseController<CaAcctEwSu, ICaAcctEwSuService>{
	@Autowired
	private ICaAcctEwSuService caAcctEwSuService;

	/**
	 * 分页列表查询
	 *
	 * @param caAcctEwSuVO
	 * @return
	 */
	@OperationLog(value = "账户预警管理-分页列表查询")
	@PostMapping(value = "/list")
	public APIResult<?> queryPageList(@RequestBody CaAcctEwSuVO caAcctEwSuVO) {
		CaAcctEwSu caAcctEwSu  = ConvertUtils.convert(CaAcctEwSu.class,caAcctEwSuVO);
		QueryWrapper<CaAcctEwSu> queryWrapper = new QueryWrapper<>();
		if (!StringUtils.isEmpty(caAcctEwSu.getEwAcctNo())){
			queryWrapper.like("EW_ACCT_NO",caAcctEwSu.getEwAcctNo());
		}
		if (!StringUtils.isEmpty(caAcctEwSu.getBankCode())){
			queryWrapper.like("BANK_CODE",caAcctEwSu.getBankCode());
		}
		Page<CaAcctEwSu> page = new Page<>(caAcctEwSuVO.getPageNo(), caAcctEwSuVO.getPageSize());
		IPage<CaAcctEwSu> pageList = caAcctEwSuService.page(page, queryWrapper);
        return APIResult.successResult(PageVo.getPageVoFromIPage(pageList));
	}

	/**
	 *   添加
	 *
	 * @param caAcctEwSuVO
	 * @return
	 */
	@OperationLog(value = "账户预警管理-添加")
	@PostMapping(value = "/add")
	public APIResult<?> add(@RequestBody CaAcctEwSuVO caAcctEwSuVO) {
		if(StringUtils.isEmpty(caAcctEwSuVO.getEwVldFlag())){
			caAcctEwSuVO.setEwVldFlag("1");
		}
		caAcctEwSuVO.setFiBcpNo("1");
		caAcctEwSuVO.setFiNo("1");
		CaAcctEwSu caAcctEwSu  = ConvertUtils.convert(CaAcctEwSu.class,caAcctEwSuVO);
		caAcctEwSuService.save(caAcctEwSu);
		return APIResult.successResult("添加成功！");
	}

	/**
	 *  编辑
	 *
	 * @param caAcctEwSuVO
	 * @return
	 */
	@OperationLog(value = "账户预警管理-编辑")
	@PostMapping(value = "/edit")
	public APIResult<?> edit(@RequestBody CaAcctEwSuVO caAcctEwSuVO) {
		caAcctEwSuVO.setFiBcpNo("1");
		caAcctEwSuVO.setFiNo("1");
		CaAcctEwSu caAcctEwSu  = ConvertUtils.convert(CaAcctEwSu.class,caAcctEwSuVO);
		caAcctEwSuService.updateById(caAcctEwSu);
		return APIResult.successResult("编辑成功!");
	}
	@OperationLog(value = "账户预警管理-是否有效")
	@PostMapping("/updateBatchById")
	public APIResult<?> updateBatchById(@RequestBody List<CaAcctEwSu> list){
	    //把失效与生效互换后修改
        List<CaAcctEwSu> caAcctEwSus = conversionEwVldFlag(list);
        caAcctEwSuService.updateBatchById(caAcctEwSus);
		return APIResult.successResult("修改状态成功！");
	}

	/**
	 * @description:  输入框查询是提示接口
	 * @param: caAcctEwSuVO
	 * @return:  模糊查询list
	 * @author liuzhonghua
	 * @date:
	 */
	@RequestMapping("queryHint")
	public APIResult<?> queryHint(@RequestBody CaAcctEwSuVO caAcctEwSuVO) {
		QueryWrapper<CaAcctEwSu> queryWrapper = new QueryWrapper<>();
		if (!StringUtils.isEmpty(caAcctEwSuVO.getEwAcctNo())){
			queryWrapper.select("DISTINCT EW_ACCT_NO").like("EW_ACCT_NO",caAcctEwSuVO.getEwAcctNo());
		}
		if (!StringUtils.isEmpty(caAcctEwSuVO.getBankCode())){
			queryWrapper.select("DISTINCT BANK_CODE").like("BANK_CODE",caAcctEwSuVO.getBankCode());
		}else {
			if ("1".equals(caAcctEwSuVO.getFlag())){
				queryWrapper.select("DISTINCT BANK_CODE");
			}else if ("2".equals(caAcctEwSuVO.getFlag())){
				queryWrapper.select("DISTINCT EW_ACCT_NO");
			}
		}
		List<CaAcctEwSu> list = caAcctEwSuService.list(queryWrapper);
		return APIResult.successResult(list);
	}

	/**
	 *   通过id删除
	 * @param
	 * @return
	 */
	@OperationLog(value = "账户预警管理-通过ewAcctNo删除")
	@PostMapping(value = "/delete")
	public APIResult<?> delete(@RequestBody List<String> list) {
		caAcctEwSuService.removeById(list.get(0));
		return APIResult.successResult("删除成功!");
	}

	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
//	@OperationLog(value = "账户预警管理-批量删除")
//	@PostMapping(value = "/deleteBatch")
//	public APIResult<?> deleteBatch(@RequestBody List<CaAcctEwSuVO> ids) {
//		this.caAcctEwSuService.removeByIds(ids.stream().map(CaAcctEwSuVO::getId).collect(Collectors.toList()));
//		return APIResult.successResult("批量删除成功!");
//	}

	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@OperationLog(value = "账户预警管理-通过id查询")
	@PostMapping(value = "/queryById")
	public APIResult<?> queryById(@RequestParam(name="id",required=true) String id) {
		CaAcctEwSu caAcctEwSu = caAcctEwSuService.getById(id);
		if(caAcctEwSu==null) {
			return APIResult.failResult(null, "未找到对应数据");
		}
		CaAcctEwSuVO caAcctEwSuVO  = ConvertUtils.convert(CaAcctEwSuVO.class,caAcctEwSu);
		return APIResult.successResult(caAcctEwSuVO);
	}

    /**
    * 导出excel
    *
    * @param
    * @param caAcctEwSuVO
    */
    @RequestMapping(value = "/exportXls")
    public void exportXls(HttpServletResponse response, CaAcctEwSuVO caAcctEwSuVO) {
        CaAcctEwSu caAcctEwSu  = ConvertUtils.convert(CaAcctEwSu.class,caAcctEwSuVO);
        super.exportXls(response, caAcctEwSu, CaAcctEwSu.class, "账户预警管理");
    }

    /**
      * 通过excel导入数据
    *
    * @param
    * @param
    * @return
    */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public APIResult<?> importExcel(@RequestParam("file") MultipartFile file) throws Exception {
        return APIResult.successResult(super.importExcel(file, CaAcctEwSu.class));
    }
    private List<CaAcctEwSu> conversionEwVldFlag(List<CaAcctEwSu> caAcctEwSu){
        for (CaAcctEwSu c:caAcctEwSu) {
            //为失效则转换为生效
            if("0".equals(c.getEwVldFlag())){
                c.setEwVldFlag("1");
                //为生效转换为失效
            }else if("1".equals(c.getEwVldFlag())){
                c.setEwVldFlag("0");
            }
        }
        return caAcctEwSu;
    }
}
